package wili.a;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author weila 2022年7月1日
 */
class Coms5 {
	public static void asert(boolean is, Object msg) {
		if (!is)
			throw new RuntimeException(msg.toString());
	}

	public static void log(int opt, Object msg) {
		if (opt < 0)
			return;
		System.out.println(msg);
	}

	public static String strOf(Object aa) {
		try {
			if (aa == null)
				return String.valueOf(null);
			return aa.toString();
		} catch (Exception e) {
			return e.toString();
		}
	}

	public static <T> String concat(String deli, T... aa) {
		StringBuilder sb = new StringBuilder(32);
		for (int i = 0; i < aa.length; i++) {
			if (i > 0 && !deli.isEmpty())
				sb.append(deli);
			sb.append(aa[i]);
		}
		return sb.toString();
	}

	static boolean isSimpleOrLangClass(Class<?> cls) {
		if (cls.isPrimitive())
			return true;
		Package pkg = cls.getPackage();
		if (pkg == null)
			return false;
		else if (pkg.getName().equals("java.lang"))
			return true;
		else if (Number.class.isAssignableFrom(cls))
			return true;
		return false;
	}

	static String stringOfArray(Object obj) {
		Class<?> cls = obj.getClass().getComponentType();
		String name = cls.getName();
		switch (name) {
		case "byte":
			return Arrays.toString((byte[]) obj);
		case "char":
			return Arrays.toString((char[]) obj);
		case "boolean":
			return Arrays.toString((boolean[]) obj);
		case "int":
			return Arrays.toString((int[]) obj);
		case "long":
			return Arrays.toString((long[]) obj);
		case "float":
			return Arrays.toString((float[]) obj);
		case "double":
			return Arrays.toString((double[]) obj);
		default:
			if (obj instanceof Object[])
				return Arrays.asList((Object[]) obj).toString();
			obj.getClass().getCanonicalName();
			return obj.toString();
		}
	}

	static TreeMap<String, Object> valueMapOfObject(Object obj) {
		Map<String, Field> fMap = new HashMap<>();
		getAll_fieldMap(obj.getClass(), fMap);
		TreeMap<String, Object> ret = valueMapOfFieldMap(fMap, obj);
		return ret;
	}

	static void getAll_fieldMap(Class<?> cls, Map<String, Field> toMap) {
		if (cls.equals(Object.class))
			return;
		Class<?> supCls = cls.getSuperclass();
		if (supCls != null) {
			getAll_fieldMap(supCls, toMap);
		}
		Field[] ff = cls.getDeclaredFields();
		for (int i = 0; i < ff.length; i++) {
			Field fd = ff[i];
			if (Boolean.FALSE && toMap.containsKey(fd.getName())) {
				// 父子类有同名属性
			} else {
				toMap.put(fd.getName(), fd);
			}
		}
	}

	public static TreeMap<String, Object> valueMapOfFieldMap(Map<String, Field> fMap, Object obj) {
		TreeMap<String, Object> ret = new TreeMap<>();
		for (Field fd : fMap.values()) {
			Object va;
			try {
				fd.setAccessible(true);
				va = fd.get(obj);
				ret.put(fd.getName(), va);
			} catch (Exception e) {
				ret.put(fd.getName(), "printErr:" + e.toString());
			}
		}
		return ret;
	}

	@SuppressWarnings("all")
	static void test() {
		int opt = 1;
		if (opt == 1) {
			System.out.println(Coms5.stringOfArray(new Integer[] { 11, 22 }));
			System.out.println(Coms5.stringOfArray(new int[] { 11, 22 }));
		}
	}

	public static String printFeildsOfBean(String deli, Object obj, List<String> fieldList) {
		StringBuilder sb = new StringBuilder();
		Class<?> cls = obj instanceof Class ? (Class) obj : obj.getClass();
		int ind = 0;
		for (String name : fieldList) {
			try {
				if (++ind > 1)
					sb.append(deli);
				Field fd = cls.getDeclaredField(name);
				Object va = fd.get(obj);
				sb.append(name + "=" + va);
			} catch (Exception e) {
				sb.append(name + "=" + e);
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		test();
	}
}
